/*
 * Tools.java
 *
 * Created on 2007��9��11��, ����4:17
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package orest.common.util;

import java.text.*;
import java.util.*;
import java.io.UnsupportedEncodingException;
import java.security.*; 

/**
 *
 * @author peng weimin
 */
public class Tools {
    
    /** Creates a new instance of Tools */
    public Tools() {
    }

     public String gettimestamp(){
      SimpleDateFormat DateFormate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sResult = DateFormate.format(new Date());
        return sResult;
 }
    
    /**
     * get current date time and formate it into yyyy-MM-dd,for example, 2007-07-02
     * and transfor the date into string and return the string.
     *
     */
    public String getDateTime1() {
        SimpleDateFormat DateFormate = new SimpleDateFormat("yyyy-MM-dd");
        String sResult = DateFormate.format(new Date());
        
        return sResult;
    }
    
    /**
     * get current date time and formate it into yyyyMMdd,for example, 2007-07-02
     * and transfor the date into string and return the string.
     *
     */
    public String getDateTime2() {
        SimpleDateFormat DateFormate = new SimpleDateFormat("yyyyMMdd");
        String sResult = DateFormate.format(new Date());
        
        return sResult;
    }
    
    /**
     * get current date time and use the value to compute the date that is one
     * week ago. Turn the date value into string and return the string.
     *
     */
    public String getIntevWeek() throws java.text.ParseException {
        Calendar calendar = Calendar.getInstance();
        /* Computer the date that is before now one week */
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyyMMdd");
        String date = bartDateFormat.format(calendar.getTime());
             
        /* return the value that formate is xxxx-xx-xx */
        return date;
        
    }
    public String gettWeek() throws java.text.ParseException {
        Calendar calendar = Calendar.getInstance();
        /* Computer the date that is before now one week */
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = bartDateFormat.format(calendar.getTime());
     
        
        /* return the value that formate is xxxx-xx-xx */
        return date;
        
    }
    /**
     * get current date time and use the value to compute the date that is one
     * month ago. Turn the date value into string and return the string.
     *
     */
    public String getIntevMonth() throws java.text.ParseException {
        Calendar calendar = Calendar.getInstance();
        /* Computer the date that is before now one week */
        calendar.add(Calendar.MONTH, -1);
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyyMMdd");
        String date = bartDateFormat.format(calendar.getTime());
       
        /* return the value that formate is xxxx-xx-xx */
        return date;
    }
    
    public String getIntevThreeMonth()  throws java.text.ParseException{
        Calendar calendar = Calendar.getInstance();
        /* Computer the date that is before now one week */
        
        calendar.add(Calendar.MONTH, -3);
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyyMMdd");
        String date = bartDateFormat.format(calendar.getTime());
        return date;
    }
    public String getThreeMonth()  throws java.text.ParseException{
        Calendar calendar = Calendar.getInstance();
        /* Computer the date that is before now one week */
        
        calendar.add(Calendar.MONTH, -3);
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = bartDateFormat.format(calendar.getTime());
        return date;
    }
    /**
     * get current date time and use the value to compute the date that is half
     * year ago. Turn the date value into string and return the string.
     *
     */
    public String getIntevHalfYear() throws java.text.ParseException {
        Calendar calendar = Calendar.getInstance();
        /* Computer the date that is before now one week */
        
        calendar.add(Calendar.MONTH, -6);
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyyMMdd");
        String date = bartDateFormat.format(calendar.getTime());
        
       
        /* return the value that formate is xxxx-xx-xx */
        return date;
    }
    
    public String getHalfYear() throws java.text.ParseException {
        Calendar calendar = Calendar.getInstance();
        /* Computer the date that is before now one week */
        
        calendar.add(Calendar.MONTH, 0);
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy");
        String date = bartDateFormat.format(calendar.getTime());
                
        /* return the value that formate is xxxx-xx-xx */
        return date;
    }
    
    public String getIntevOneYear()  throws java.text.ParseException{
        Calendar calendar = Calendar.getInstance();
        /* Computer the date that is before now one week */
        
        calendar.add(Calendar.YEAR, -1);
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyyMMdd");
        String date = bartDateFormat.format(calendar.getTime());
        return date;
    }
    
    
    public String getInteAll()  throws java.text.ParseException{
        Calendar calendar = Calendar.getInstance();
        /* Computer the date that is before now one week */
        
        calendar.add(Calendar.YEAR, -20);
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyyMMdd");
        String date = bartDateFormat.format(calendar.getTime());
        return date;
    }
    
    /**
     * ISO646-US��Unicode collection
     */
    public static final String US_ASCII = "US-ASCII";
    
    /**
     * ISO-LATIN-1 code collection
     */
    public static final String ISO_8859_1 = "ISO-8859-1";
    
    /** utf-8 */
    public static final String UTF_8 = "UTF-8";
    
    /**
     * utf-16 and big endian
     */
    public static final String UTF_16BE = "UTF-16BE";
    
    /**
     * utf-16 and Little-endian
     */
    public static final String UTF_16LE = "UTF-16LE";
    
    /**
     * utf-16 and auto select endian
     */
    public static final String UTF_16 = "UTF-16";
    
    /**
     * gb2312
     */
    public static final String GBK = "GB2312";
    
    /**
     * cp850
     */
    public static final String cp = "cp850";
    
    /**
     * use the US-ASCII to encoding the string
     */
    public String toASCII(String str) throws UnsupportedEncodingException {
        return this.changeCharset(str, US_ASCII);
    }
    
    /**
     * use the ISO-8859-1 to encoding the string
     */
    public String toISO_8859_1(String str) throws UnsupportedEncodingException {
        return this.changeCharset(str, ISO_8859_1);
    }
    
    /**
     * use the UTF-8 to encoding the string
     */
    public String toUTF_8(String str) throws UnsupportedEncodingException {
        return this.changeCharset(str, UTF_8);
    }
    
    /**
     * use the UTF-16BE to encoding the string
     */
    public String toUTF_16BE(String str) throws UnsupportedEncodingException {
        return this.changeCharset(str, UTF_16BE);
    }
    
    /**
     * use the UTF-16LE to encoding the string
     */
    public String toUTF_16LE(String str) throws UnsupportedEncodingException {
        return this.changeCharset(str, UTF_16LE);
    }
    
    /**
     * use the UTF-16 to encoding the string
     */
    public String toUTF_16(String str) throws UnsupportedEncodingException {
        return this.changeCharset(str, UTF_16);
    }
    
    /**
     * use the GB2312 to encoding the string
     */
    public String toGBK(String str) throws UnsupportedEncodingException {
        return this.changeCharset(str, GBK);
    }

     public String toGBKn(String str) throws UnsupportedEncodingException {
        return this.changeCharset(str, "GBK");
    }
    
    /**
     * use the GB2312 to encoding the string
     */
    public String toCP(String str) throws UnsupportedEncodingException {
        return this.changeURLCharset(str, cp);
    }
    
    public String toCPFromUTF(String str) throws UnsupportedEncodingException {
        return this.changeCharsetFromUTF(str, cp);
    }
    
    /**
     * use the GB2312 to encoding the string
     */
    public String toGBKForURL(String str) throws UnsupportedEncodingException {
        return this.changeURLCharset(str, GBK);
    }
    
    /**
     * This is the function to change the string's encoding
     */
    private String changeCharset(String str, String newCharset)
    throws UnsupportedEncodingException {
        if (str != null) {
            /* get string use default encoding */
            byte[] bs = str.getBytes("gbk");
           //  byte[] bs = str.getBytes("cp850");
            /* use new charset encoding the string */
            return new String(bs, newCharset);
        }
        return null;
    }
    
    /**
     * This is the function to change the string's encoding
     */
    private String changeURLCharset(String str, String newCharset)
    throws UnsupportedEncodingException {
        if (str != null) {
            /* get string use default encoding */
            byte[] bs = str.getBytes("gb2312");
            /* use new charset encoding the string */
            return new String(bs, newCharset);
        }
        return null;
    }
    
    private String changeCharsetFromUTF(String str, String newCharset)
    throws UnsupportedEncodingException {
        if (str != null) {
            /* get string use default encoding */
            byte[] bs = str.getBytes("UTF-8");
            /* use new charset encoding the string */
            return new String(bs, newCharset);
        }
        return null;
    }
    
    public final static String md5(String s) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; 
        try { 
            byte[] strTemp = s.getBytes(); 
            MessageDigest mdTemp = MessageDigest.getInstance("MD5"); 
            mdTemp.update(strTemp); 
            byte[] md = mdTemp.digest(); 
            int j = md.length; 
            char str[] = new char[j * 2]; 
            int k = 0; 
            for (int i = 0; i < j; i++) { 
                byte byte0 = md[i]; 
                str[k++] = hexDigits[byte0 >>> 4 & 0xf]; 
                str[k++] = hexDigits[byte0 & 0xf]; 
            } 

            return new String(str); 
        } 
        catch (Exception e){
            e.printStackTrace();
            return null; 
        } 
    }
    
    public static String parseNILL(String src) {
        if(src == null)
            return "";
        else
            return src.trim();
    }
}
